package com.hxy.network.core.netty;

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import com.hxy.network.core.netty.client.NettyClient;
import com.hxy.network.core.netty.server.ServerHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @description:
 * @program: component_code_integration
 * @author: HCY
 * @create: 2022/12/07 15:01
 */
@RestController
@RequestMapping("/netty")
@Slf4j
public class NettyController {

    @Resource
    private NettyClient clientStart;
    @RequestMapping(value="/equipment")
    public  String equipment(String url,String data){

        if(StrUtil.isNotEmpty(url)){
            //执行设备控制   根据product.getUrl() 上个类写入map  的key  取到map中的 ChannelHandlerContext  执行writeAndFlush发送数据
            ServerHandler.map.get(url).channel().writeAndFlush(data);
            return "成功";
        }else{
            return  "失败";
        }
    }

    /**
     * 客户端向netty服务发送数据
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/nettyClient")
    public String nettyClient(String data) throws Exception {

        clientStart.send(data);
        return "发送成功";
    }

}

